package com.wanxi.servlet.user;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.model.ResultModel;
import com.wanxi.model.UserModel;
import com.wanxi.service.UserService;
import com.wanxi.service.impl.UserServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/user/findAll")
public class UserFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        1.乱码处理
//        req.setCharacterEncoding("UTF-8");
//        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的值
//        page 第几页 limit 每一页显示的条数
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String username = req.getParameter("username");
        String birthdayStart = req.getParameter("birthdayStart");
        String birthdayEnd = req.getParameter("birthdayEnd");
        String updateTimeStart = req.getParameter("updateTimeStart");
        String updateTimeEnd = req.getParameter("updateTimeEnd");
        String enable = req.getParameter("enable");
        String address = req.getParameter("address");

//        3.封装到model
        UserModel model = new UserModel();
        model.setPage(Tool.strToPage(page));
        model.setLimit(Tool.strToLimit(limit));
        model.setName(Tool.nullToStr(username));
        model.setEnable(Tool.strToInt(enable));
        model.setAddress(Tool.nullToStr(address));
        model.setUpdateTimeStart(Tool.nullToDate(updateTimeStart));
        model.setUpdateTimeEnd(Tool.nullToDate(updateTimeEnd));
        model.setBirthdayStart(Tool.nullToDate(birthdayStart));
        model.setBirthdayEnd(Tool.nullToDate(birthdayEnd));
//        4.调用service
        UserService userService = new UserServiceImpl();
//        5.得到返回值
        ResultModel resultModel = userService.findAll(model);
//        6.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultModel));

    }
}
